const { ccclass, property } = cc._decorator;
import GbzLevel from "../game/GbzLevel";
import { ENUM_CLIP, ENUM_STATUS, ENUM_UI } from "../GbzEnum";
import GbzInstance from "../GbzInstance";
import { getNumFromStr, random } from "../GbzUtils";
import GbzAudioManager from "./GbzAudioManager";
import GbzDataManager from "./GbzDataManager";
import GbzPoolManager from "./GbzPoolManager";
import GbzToastManager from "./GbzToastManager";

@ccclass
export default class GbzGameManager extends cc.Component {

    stage: cc.Node = null
    levelComp: GbzLevel = null

    onLoad() {
        GbzInstance.setGameMgr(this)
        this.stage = cc.find('Stage', this.node)
    }

    onGameStart() {
        GbzDataManager.ins.reset()
        this.init()
    }

    onGameSkill() {
        GbzDataManager.ins.isSkill = true
        if (GbzDataManager.ins.skillIndex == 0) {
            GbzAudioManager.ins.play(ENUM_CLIP.SKILL_UNLOCK)
            const targetLock = this.levelComp.getTargetLock()
            if (targetLock) {
                cc.tween(targetLock).to(0.2, { scale: 0 }).call(() => {
                    GbzDataManager.ins.isSkill = false
                    this.levelComp.onTargetLockDestroy(targetLock)
                }).start()
            }
        } else if (GbzDataManager.ins.skillIndex == 1) {
            GbzAudioManager.ins.play(ENUM_CLIP.SKILL_CLEAR)
            const screws = this.levelComp.getTargetScrews()
            screws.forEach(screw => {
                const index = getNumFromStr((screw.getComponent(cc.Sprite).spriteFrame)._name)
                this.levelComp.screwRefreshArr.push(index)
                cc.tween(screw).to(0.2, { scale: 0 }).start()
            })
            this.scheduleOnce(() => {
                GbzDataManager.ins.isSkill = false
                this.levelComp.onTargetClear()
            }, 0.2)
        } else if (GbzDataManager.ins.skillIndex == 2) {
            GbzToastManager.ins.show('请选择你要融化的挡板', { gravity: 'CENTER', bg_color: cc.color(102, 202, 28, 255) })
        }
    }

    onGameOver(ui: ENUM_UI) {
        debugger
        if (GbzDataManager.ins.status == ENUM_STATUS.UNRUN) return
        GbzDataManager.ins.status = ENUM_STATUS.UNRUN
        GbzInstance.uiMgr.toggle(ENUM_UI.LAYER_EXIT, false)
        GbzInstance.uiMgr.toggle(ENUM_UI.LAYER_TIP, false)
        if (ui == ENUM_UI.LAYER_LOSE_FULL || ui == ENUM_UI.LAYER_LOSE_TIME) {
            GbzAudioManager.ins.play(ENUM_CLIP.GAME_WIN)
        } else {
            GbzAudioManager.ins.play(ENUM_CLIP.GAME_WIN)
            
            GbzDataManager.ins.game_level += 1
            if (GbzDataManager.ins.game_level > GbzDataManager.ins.game_level_max) {
                GbzDataManager.ins.game_level_current = random(1, GbzDataManager.ins.game_level_max)
            } else {
                GbzDataManager.ins.game_level_current = GbzDataManager.ins.game_level
            }
            GbzDataManager.ins.store()
            this.init()
        }
        this.scheduleOnce(() => {
            GbzInstance.uiMgr.toggle(ui)
        }, 0.5)
    }

    onGameRevive() {
        const screws = this.levelComp.getTargetScrews()
        debugger
        screws.forEach(screw => {
            debugger
            const index = getNumFromStr((screw.getComponent(cc.Sprite).spriteFrame)._name)
            this.levelComp.screwRefreshArr.push(index)
            cc.tween(screw).to(0.2, { scale: 0 }).start()
        })
        this.scheduleOnce(() => {
            this.levelComp.onTargetClear()
        }, 0.2)
    }

    onGameCheck() {
        
        // console.log('当前游戏进度检测')
        if (this.levelComp.checkBoxClear()) {
            this.onGameOver(ENUM_UI.LAYER_WIN)
            return
        }
        if (this.levelComp.checkTargetFull()) {
            this.onGameOver(ENUM_UI.LAYER_LOSE_FULL)
            return
        }
    }

    async init() {
        GbzDataManager.ins.status = ENUM_STATUS.UNRUN
        this.stage.removeAllChildren()
        const levelNode = GbzPoolManager.ins.getNode(`GbzLevel${GbzDataManager.ins.game_level_current}`, this.stage)
        this.levelComp = levelNode.getComponent(GbzLevel)
        this.levelComp.init()
        GbzInstance.uiMgr.setCurrentLevel()
        GbzInstance.uiMgr.setMainCountDown(true)
        GbzDataManager.ins.status = ENUM_STATUS.RUN
        await GbzInstance.fadeMgr.out()
    }
}
